package org.example.DAO.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.example.DAO.ClassInfoDAO;
import org.example.Entity.ClassInfo;
import org.example.Utill.DatabaseUtil;

public class ClassInfoDAOImpl implements ClassInfoDAO {
    @Override
    // 添加班级
    public boolean addClass(ClassInfo classObj) {
        String sql = "INSERT INTO class (class_id, class_name) VALUES (?, ?)";
        try (Connection conn = DatabaseUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            
            pstmt.setString(1, classObj.getClassID());
            pstmt.setString(2, classObj.getClassName());
            return pstmt.executeUpdate() > 0;
            
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }
    @Override
    // 获取所有班级
    public List<ClassInfo> getAllClasses() {
        List<ClassInfo> classes = new ArrayList<>();
        String sql = "SELECT * FROM class";
        
        try (Connection conn = DatabaseUtil.getConnection();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(sql)) {
            
            while (rs.next()) {
                ClassInfo classObj = new ClassInfo();
                classObj.setClassID(rs.getString("class_id"));
                classObj.setClassName(rs.getString("class_name"));
                classes.add(classObj);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return classes;
    }
    @Override
    
    // 根据班级名称获取班级ID
    public String getClassIdByName(String className) {
        String sql = "SELECT class_id FROM class WHERE class_name = ?";
        try (Connection conn = DatabaseUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            
            pstmt.setString(1, className);
            ResultSet rs = pstmt.executeQuery();
            
            if (rs.next()) {
                return rs.getString("class_id");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    @Override
    public boolean updateClassName(String currentClassName, String trim) {
        String sql = "UPDATE class SET class_name = ? WHERE class_name = ?";
        try (Connection conn = DatabaseUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setString(1, trim);
            pstmt.setString(2, currentClassName);
            return pstmt.executeUpdate() > 0;

        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }
    @Override

    public boolean deleteClassByName(String currentClassName) {
        String sql = "DELETE FROM class WHERE class_name = ?";
        try (Connection conn = DatabaseUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setString(1, currentClassName);
            return pstmt.executeUpdate() > 0;

        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }
    public boolean addClassInfo(ClassInfo newClass) {
        String sql = "INSERT INTO class (class_id, class_name) VALUES (?, ?)";
        try (Connection conn = DatabaseUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setString(1, newClass.getClassID());
            pstmt.setString(2, newClass.getClassName());
            return pstmt.executeUpdate() > 0;

        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }
}
